/**
 * 订单相关工具函数
 */

/**
 * 获取订单状态文本
 * @param {number} status 订单状态
 * @returns {string} 状态文本
 */
export function getOrderStatusText(status) {
    const statusMap = {
        0: '未支付',
        1: '已支付',
        2: '已发货',
        3: '已完成',
        4: '已关闭',
        5: '已取消',
        6: '退款中',
        7: '已退款'
    };
    return statusMap[status] || '未知状态';
}

/**
 * 获取订单状态样式类
 * @param {number} status 订单状态
 * @returns {string} 样式类名
 */
export function getOrderStatusClass(status) {
    const classMap = {
        0: 'unpaid',
        1: 'pending',
        2: 'pending',
        3: 'completed',
        4: 'closed',
        5: 'cancelled',
        6: 'refunding',
        7: 'refunded'
    };
    return classMap[status] || 'unknown';
}

/**
 * 格式化订单时间
 * @param {string} timeString 时间字符串
 * @returns {string} 格式化后的时间
 */
export function formatOrderTime(timeString) {
    if (!timeString) return '';

    const date = new Date(timeString);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}`;
}

/**
 * 计算剩余支付时间
 * @param {string} createTime 创建时间
 * @param {number} expireHours 过期小时数，默认24小时
 * @returns {string} 剩余时间文本
 */
export function getTimeLeft(createTime, expireHours = 24) {
    if (!createTime) return '';

    const created = new Date(createTime);
    const expired = new Date(created.getTime() + expireHours * 60 * 60 * 1000);
    const now = new Date();

    if (now >= expired) {
        return '已超时';
    }

    const diff = expired - now;
    const hours = Math.floor(diff / (1000 * 60 * 60));
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));

    if (hours > 0) {
        return `${hours}小时${minutes}分钟`;
    } else {
        return `${minutes}分钟`;
    }
}

/**
 * 获取订单类型
 * @param {number} index 索引
 * @returns {string} 订单类型
 */
export function getOrderType(index) {
    const types = ['unpaid', 'pending', 'completed', 'other'];
    return types[index] || 'other';
}

/**
 * 获取空状态图标
 * @param {number} index 索引
 * @returns {string} 图标名称
 */
export function getEmptyIcon(index) {
    const icons = ['shopping-cart', 'list', 'checkmark-circle', 'close-circle'];
    return icons[index] || 'close-circle';
}

/**
 * 获取空状态文本
 * @param {number} index 索引
 * @returns {string} 空状态文本
 */
export function getEmptyText(index) {
    const texts = ['暂无待付款订单', '暂无待使用订单', '暂无已完成订单', '暂无其他订单'];
    return texts[index] || '暂无订单';
}

/**
 * 验证订单数据
 * @param {Object} order 订单对象
 * @returns {boolean} 是否有效
 */
export function validateOrder(order) {
    if (!order || typeof order !== 'object') {
        return false;
    }

    const requiredFields = ['id', 'orderNo', 'status'];
    return requiredFields.every(field => order.hasOwnProperty(field));
}

/**
 * 格式化订单金额
 * @param {number|string} amount 金额
 * @returns {string} 格式化后的金额
 */
export function formatOrderAmount(amount) {
    if (!amount && amount !== 0) return '0.00';
    const num = parseFloat(amount);
    return isNaN(num) ? '0.00' : num.toFixed(2);
}

/**
 * 获取订单操作按钮配置
 * @param {string} orderType 订单类型
 * @param {number} status 订单状态
 * @returns {Array} 按钮配置数组
 */
export function getOrderActions(orderType, status) {
    const actionMap = {
        unpaid: [
            { text: '取消订单', type: 'info', plain: true, action: 'cancel-order' },
            { text: '立即支付', type: 'warning', plain: false, action: 'go-to-pay' }
        ],
        pending: [
            { text: '联系客服', type: 'info', plain: true, action: 'contact-service' },
            { text: '查看详情', type: 'primary', plain: true, action: 'view-detail' }
        ],
        completed: [
            { text: '联系客服', type: 'info', plain: true, action: 'contact-service' },
            { text: '查看详情', type: 'primary', plain: true, action: 'view-detail' },
            { text: '申请退款', type: 'warning', plain: true, action: 'request-refund' }
        ],
        other: [
            ...(status === 6 ? [{ text: '取消退款', type: 'info', plain: true, action: 'cancel-refund' }] : []),
            { text: '联系客服', type: 'info', plain: true, action: 'contact-service' },
            { text: '查看详情', type: 'primary', plain: true, action: 'view-detail' }
        ]
    };

    return actionMap[orderType] || actionMap.other;
}
